package com.detrum.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


import com.detrum.bean.FileBean;

import java.util.ArrayList;

/**
 * Created by abc on 2017/12/6.
 */

public class SqliteMediaOperation {
    private final String TAG="SqliteOperation";
    private static SqliteMediaOperation sqliteMediaOperation;
    private static SQLiteDatabase sqLiteDatabaseWrite,sqLiteDatabaseRead;
    private String table1 = "fileList";
    private String table1_key_fileName = "fileName";
    private String table1_key_time = "time";
    private String table1_key_cachepath = "cachePath";
    private String table1_key_thumbPath = "thumbPath";
    private String table1_key_nfsPath = "nfsPath";
    private String table1_key_fileTime = "fileTime";
    private String table1_key_position = "position";
    private String table1_key_size = "size";
    private String table1_key_isNfs = "isNfs";
    private String table1_key_isLocal = "isLocal";

    private static long currentLocal;
    private static long currentNfs;

    public SqliteMediaOperation(Context context) {
        sqLiteDatabaseWrite = SqliteHelper.getInstance(context).getWritableDatabase();
        sqLiteDatabaseRead = SqliteHelper.getInstance(context).getReadableDatabase();
    }

    public static SqliteMediaOperation getInstence(Context context){
            if(sqliteMediaOperation ==null){
                sqliteMediaOperation = new SqliteMediaOperation(context);
            }
            synchronized (sqliteMediaOperation){
                return sqliteMediaOperation;
            }
    }
    public long Insert(FileBean fileBean){
        if(fileBean.getFileName()==null){
            return -1;
        }
        ContentValues contentValues = new ContentValues();
        contentValues.put(table1_key_cachepath,fileBean.getCachePath());
        contentValues.put(table1_key_thumbPath,fileBean.getThumbPath());
        contentValues.put(table1_key_size,fileBean.getSize());
        contentValues.put(table1_key_isLocal,fileBean.getIsLocal());
        contentValues.put(table1_key_nfsPath,fileBean.getNfsPath());
        contentValues.put(table1_key_fileTime,fileBean.getFileTime());
        contentValues.put(table1_key_position,fileBean.getPosition());
        contentValues.put(table1_key_isNfs,fileBean.getIsNfs());
        contentValues.put(table1_key_time,fileBean.getTime());
        int count = SelectFileNum(fileBean.getFileName());
        if(count>0){
            long updata = UpdateItem(fileBean);
            return updata;
        }
        contentValues.put(table1_key_fileName,fileBean.getFileName());
        long insert = sqLiteDatabaseWrite.insert(table1,null,contentValues);
        return insert;
    }

    public FileBean SelectSingle(String fileName){
        Cursor cursor = sqLiteDatabaseRead.query(table1,null,table1_key_fileName + " = ?",new String[]{fileName},null,null,null);
        FileBean fileBean = null;
        if(cursor.moveToFirst()){
            String cachePath = cursor.getString(cursor.getColumnIndex(table1_key_cachepath));
            String thumbPath = cursor.getString(cursor.getColumnIndex(table1_key_thumbPath));
            String nfsPath = cursor.getString(cursor.getColumnIndex(table1_key_nfsPath));
            String time = cursor.getString(cursor.getColumnIndex(table1_key_time));
            int size = cursor.getInt(cursor.getColumnIndex(table1_key_size));
            int position = cursor.getInt(cursor.getColumnIndex(table1_key_position));
            int isNfs = cursor.getInt(cursor.getColumnIndex(table1_key_isNfs));
            int isLocal = cursor.getInt(cursor.getColumnIndex(table1_key_isLocal));
            long fileTime = cursor.getLong(cursor.getColumnIndex(table1_key_fileTime));
            fileBean = new FileBean(nfsPath,fileName,time,cachePath,thumbPath,size,fileTime,position,isLocal,isNfs);
        }
        return fileBean;
    }
    public int UpdateItem(FileBean fileBean){
        String fileName = fileBean.getFileName();
        if(fileName == null){
            return -1;
        }
        ContentValues contentValues = new ContentValues();
        String cachePath = fileBean.getCachePath();
        if(cachePath!=null){
            contentValues.put(table1_key_cachepath,cachePath);
        }
        String thumbPath = fileBean.getThumbPath();
        if(thumbPath!=null){
            contentValues.put(table1_key_thumbPath,thumbPath);
        }
        String nfsPath = fileBean.getNfsPath();
        if(nfsPath!=null){
            contentValues.put(table1_key_nfsPath,nfsPath);
        }
        long fileTime = fileBean.getFileTime();
        if(fileTime!=0){
            contentValues.put(table1_key_fileTime,fileTime);
        }
        int isNfs = fileBean.getIsNfs();
        if(isNfs!=10){
            contentValues.put(table1_key_isNfs,isNfs);
        }
        int isLocal = fileBean.getIsLocal();
        if(isLocal!=10){
            contentValues.put(table1_key_isLocal,isLocal);
        }
        long size = fileBean.getSize();
        if(size!=0){
            contentValues.put(table1_key_size,size);
        }
        String time = fileBean.getTime();
        if(time!=null){
            contentValues.put(table1_key_time,time);
        }
        int count = contentValues.size();
        if(count==0){
            return -2;
        }
        int update = sqLiteDatabaseWrite.update(table1,contentValues,table1_key_fileName + " = ?",new String[]{fileName});
        return update;
    }
    public ArrayList<FileBean> SelectLocal(int page,int Local){
        ArrayList<FileBean> listBean = new ArrayList<>();
        if(page==1){
            currentLocal= Long.MAX_VALUE;
        }
        String[]isLocals={Local+"",currentLocal+""};
        Cursor cursor = sqLiteDatabaseRead.query(table1,null,table1_key_isLocal+" = ? and "+table1_key_fileTime+" < ?",isLocals,null,null,table1_key_fileTime+" DESC","10");
        if(cursor.getColumnCount()!=0){
            while (cursor.moveToNext()){
                int isLocal = cursor.getInt(cursor.getColumnIndex(table1_key_isLocal));
                int size = cursor.getInt(cursor.getColumnIndex(table1_key_size));
                String fileName = cursor.getString(cursor.getColumnIndex(table1_key_fileName));
                String cachePath = cursor.getString(cursor.getColumnIndex(table1_key_cachepath));
                String nfsPath = cursor.getString(cursor.getColumnIndex(table1_key_nfsPath));
                String thumbPath = cursor.getString(cursor.getColumnIndex(table1_key_thumbPath));
                long fileTime = cursor.getLong(cursor.getColumnIndex(table1_key_fileTime));
                int position = cursor.getInt(cursor.getColumnIndex(table1_key_position));
                int isNfs = cursor.getInt(cursor.getColumnIndex(table1_key_isNfs));
                String time = cursor.getString(cursor.getColumnIndex(table1_key_time));
                FileBean fileBean = new FileBean(nfsPath,fileName,time,cachePath,thumbPath,size,fileTime,position,isLocal,isNfs);
                listBean.add(fileBean);
                if(cursor.isLast()){
                    currentLocal = fileTime;
                }
            }
        }
        cursor.close();
        return listBean;
    }
    public ArrayList<FileBean> SelectNfs(int page,int nfsFlag){
        ArrayList<FileBean> listBean = new ArrayList<>();
        if(page==1){
            currentNfs=Long.MAX_VALUE;
        }
        String[]isNfss={nfsFlag+"",currentNfs+""};
        Log.e(TAG,"currentPosition = "+currentNfs);
        Cursor cursor = sqLiteDatabaseRead.query(table1,null,table1_key_isNfs+" = ? and "+table1_key_fileTime+" < ?",isNfss,null,null,table1_key_fileTime+" DESC","10");
        if(cursor.getColumnCount()!=0){
            while (cursor.moveToNext()){
                int isLocal = cursor.getInt(cursor.getColumnIndex(table1_key_isLocal));
                int size = cursor.getInt(cursor.getColumnIndex(table1_key_size));
                String fileName = cursor.getString(cursor.getColumnIndex(table1_key_fileName));
                String cachePath = cursor.getString(cursor.getColumnIndex(table1_key_cachepath));
                String nfsPath = cursor.getString(cursor.getColumnIndex(table1_key_nfsPath));
                String thumbPath = cursor.getString(cursor.getColumnIndex(table1_key_thumbPath));
                long fileTime = cursor.getLong(cursor.getColumnIndex(table1_key_fileTime));
                int position = cursor.getInt(cursor.getColumnIndex(table1_key_position));
                int isNfs = cursor.getInt(cursor.getColumnIndex(table1_key_isNfs));
                String time = cursor.getString(cursor.getColumnIndex(table1_key_time));
                FileBean fileBean = new FileBean(nfsPath,fileName,time,cachePath,thumbPath,size,fileTime,position,isLocal,isNfs);
                listBean.add(fileBean);
                if(cursor.isLast()){
                    currentNfs = fileTime;
                }
            }
        }
        cursor.close();
        return listBean;
    }
    public int SelectFileNum(String fileName){
        Cursor cursor = sqLiteDatabaseRead.query(table1,null,table1_key_fileName+" = ?",new String[]{fileName},null,null,null);
        int count = cursor.getCount();
        fileName = fileName.replace(".jpg",".mp4");
        Cursor cursor1 = sqLiteDatabaseRead.query(table1,null,table1_key_fileName+" = ?",new String[]{fileName},null,null,null);
        count += cursor1.getCount();
        cursor.close();
        cursor1.close();
        return count;
    }
    public int[] getFlags(String fileName){
        Cursor cursor = sqLiteDatabaseRead.query(table1,null,table1_key_fileName+" = ?",new String[]{fileName},null,null,null);
        int[] flags = new int[2];
        if(cursor.moveToFirst()){
            int isNfs = cursor.getInt(cursor.getColumnIndex(table1_key_isNfs));
            int isLocal = cursor.getInt(cursor.getColumnIndex(table1_key_isLocal));
            flags[0] = isLocal;
            flags[1] = isNfs;
        }
        cursor.close();
        return flags;
    }
    public int DeleteLocalAll(){
        return sqLiteDatabaseWrite.delete(table1,table1_key_fileName +"!= ?",new String[]{"a"});
    }
    public ArrayList<FileBean> getDeleteFiles(){
        Cursor cursor = sqLiteDatabaseRead.query(table1,null,table1_key_isNfs+" != ? and "+table1_key_isLocal+" == ?",new String[]{1+"",0+""},null,null,null);
        int count = cursor.getCount();
        if(count==0){
            return null;
        }else{
            ArrayList<FileBean>listBean = new ArrayList<>();
            while (cursor.moveToNext()){
                int isLocal = cursor.getInt(cursor.getColumnIndex(table1_key_isLocal));
                int size = cursor.getInt(cursor.getColumnIndex(table1_key_size));
                String fileName = cursor.getString(cursor.getColumnIndex(table1_key_fileName));
                String cachePath = cursor.getString(cursor.getColumnIndex(table1_key_cachepath));
                String nfsPath = cursor.getString(cursor.getColumnIndex(table1_key_nfsPath));
                String thumbPath = cursor.getString(cursor.getColumnIndex(table1_key_thumbPath));
                long fileTime = cursor.getLong(cursor.getColumnIndex(table1_key_fileTime));
                int position = cursor.getInt(cursor.getColumnIndex(table1_key_position));
                int isNfs = cursor.getInt(cursor.getColumnIndex(table1_key_isNfs));
                String time = cursor.getString(cursor.getColumnIndex(table1_key_time));
                FileBean fileBean = new FileBean(nfsPath,fileName,time,cachePath,thumbPath,size,fileTime,position,isLocal,isNfs);
                listBean.add(fileBean);
            }
            return listBean;
        }
    }
    public String getFilePath(String fileName){
        String filaPath=null;
        Cursor cursor = sqLiteDatabaseRead.query(table1,new String[]{table1_key_cachepath},table1_key_fileName+" = ?",new String[]{fileName},null,null,null);
        if(cursor.getCount()!=0&&cursor.moveToFirst()){
            filaPath = cursor.getString(cursor.getColumnIndex(table1_key_cachepath));
        }
        cursor.close();
        return filaPath;
    }
    public String getNfsPath(String fileName){
        String filaPath=null;
        Cursor cursor = sqLiteDatabaseRead.query(table1,new String[]{table1_key_nfsPath},table1_key_fileName+" = ?",new String[]{fileName},null,null,null);
        if(cursor.getCount()!=0&&cursor.moveToFirst()){
            filaPath = cursor.getString(cursor.getColumnIndex(table1_key_nfsPath));
        }
        cursor.close();
        return filaPath;
    }
    public String getThumbPath(String fileName){
        String thumbPath = null;
        Cursor cursor = sqLiteDatabaseRead.query(table1,new String[]{table1_key_thumbPath},table1_key_fileName+" = ?",new String[]{fileName},null,null,null);
        if(cursor.getCount()!=0&&cursor.moveToFirst()){
            thumbPath = cursor.getString(cursor.getColumnIndex(table1_key_thumbPath));
        }
        cursor.close();
        return thumbPath;
    }


    public int UpdataItemLocal(int isLocal,String fileName){
        ContentValues contentValues = new ContentValues();
        contentValues.put(table1_key_isLocal,isLocal);
        int aa = sqLiteDatabaseWrite.update(table1,contentValues,table1_key_fileName+" = ?",new String[]{fileName});
        return aa;
    }
    public int UpdataItemNfs(FileBean fileBean,String fileName){
        ContentValues contentValues = new ContentValues();
        contentValues.put(table1_key_isNfs,fileBean.getIsNfs());
        contentValues.put(table1_key_nfsPath,fileBean.getNfsPath());
        contentValues.put(table1_key_size,fileBean.getSize());
        contentValues.put(table1_key_fileTime,fileBean.getFileTime());
        int aa = sqLiteDatabaseWrite.update(table1,contentValues,table1_key_fileName+" = ?",new String[]{fileName});
        return aa;
    }
    public int UpdataItemIsNfs(String fileName,int isNfs){
        ContentValues contentValues = new ContentValues();
        contentValues.put(table1_key_isNfs,isNfs);
        int aa = sqLiteDatabaseWrite.update(table1,contentValues,table1_key_fileName+" = ?",new String[]{fileName});
        return aa;
    }
    public int UpdataItemThumbPath(String thumbPath,String fileName){
        ContentValues contentValues = new ContentValues();
        contentValues.put(table1_key_thumbPath,thumbPath);
        int thumb = sqLiteDatabaseWrite.update(table1,contentValues,table1_key_fileName+" = ?",new String[]{fileName});
        return thumb;
    }
    public void UpdataItemTime(String fileName, String time){
        if(time!=null){
            ContentValues contentValues = new ContentValues();
            contentValues.put(table1_key_time,time);
            sqLiteDatabaseWrite.update(table1,contentValues,table1_key_fileName+" = ?",new String[]{fileName});
        }
    }
    public int UpdataAllNfs(int oldNfs ,int isNfs){
        ContentValues contentValues = new ContentValues();
        contentValues.put(table1_key_isNfs,isNfs);
        int aa = sqLiteDatabaseWrite.update(table1,contentValues,table1_key_isNfs+" = ?",new String[]{oldNfs+""});
        return aa;
    }
    public int UpdataItem(FileBean fileBean, String fileName){
        ContentValues contentValues = new ContentValues();
        contentValues.put(table1_key_cachepath,fileBean.getCachePath());
        contentValues.put(table1_key_size,fileBean.getSize());
        int aa = sqLiteDatabaseWrite.update(table1,contentValues,table1_key_fileName+" = ?",new String[]{fileName});
        return aa;
    }
    public int UpdataDownloadItem(String cachePath, String fileName){
        ContentValues contentValues = new ContentValues();
        contentValues.put(table1_key_cachepath,cachePath);
        contentValues.put(table1_key_isLocal,1);
        int aa = sqLiteDatabaseWrite.update(table1,contentValues,table1_key_fileName+" = ?",new String[]{fileName});
        return aa;
    }
    public int DeleteItem(String fileName){
        return sqLiteDatabaseWrite.delete(table1,table1_key_fileName+" = ?",new String[]{fileName});
    }
}
